package com.atguigu.srb.core.controller.api;


import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.core.entity.UserInfo;
import com.atguigu.srb.core.entity.vo.UserCenterIndexVo;
import com.atguigu.srb.core.entity.vo.UserInfoVo;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-06-30
 */
@RestController
//开启跨域
// @CrossOrigin
@Api(tags = "用户登录和注册模块")
@RequestMapping("/api/core/userInfo")
public class ApiUserInfoController {
    @Resource
    UserInfoService userInfoService;

    //完善微信登录的用户的个人信息
    @ApiOperation("完善微信登录用户的个人信息")
    @PutMapping("updateInfo/{mobile}/{userType}")
    //TODO 为什么这个地方的token是非必须的,如果获取不到token,根本就不会发起这个请求,不是吗
    public R updateInfo(@RequestHeader(value = "token",required = false)String token,
                        @PathVariable("mobile")String mobile,
                        @PathVariable("userType")Integer userType){
        UserInfo userInfo = userInfoService.updateUserInfo(token,mobile,userType);
        //将更新后的userInfo返回给前端,用来做数据的回显以及左侧信息的展示
        return R.ok().data("item",userInfo);
    }
    //用来确认用户的手机号是否已经注册过了
    @ApiOperation("检查手机号是否有注册过")
    @GetMapping("checkMobile/{mobile}")
    public R checkMobile(@PathVariable("mobile")String mobile){
        int count = userInfoService.count(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getMobile, mobile));
        return R.ok().data("isRegistered",count > 0);
    }
    @ApiOperation("获取个人中心中所需的用户的信息")
    @GetMapping("auth/userCenterIndexVO")
    public R getUserInfo(@RequestHeader("token")String token){
        //根据token获取到用户的相关的信息,然后去数据库中查询数据并最终返回
        UserCenterIndexVo userCenterIndexVo = userInfoService.getUserInfo(token);
        return R.ok().data("item",userCenterIndexVo);
    }
    @ApiOperation("用户注册")
    @PostMapping("/registry")
    public R registry(@RequestBody UserInfoVo userInfoVo){
        //前端传过来的userInfo对象一共有三个参数,号码,短信验证码,密码
        userInfoService.registry(userInfoVo);
        return R.ok();
    }
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R login(@RequestBody UserInfo userInfo, HttpServletRequest request){
        String token = userInfoService.login(userInfo,request);
        return R.ok().data("token",token);
    }
    @ApiOperation("首页查询会员登录数据回显")
    @GetMapping("auth/getInfo")
    public R getInfo(@RequestHeader(value = "token",required = false)String token){
        UserInfo userInfo = userInfoService.getInfo(token);
        return R.ok().data("userInfo",userInfo);
    }
}

